﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MvCameraControl;

namespace CLVisionCameraControl
{
    //HIK面阵相机
    public class HIKAreaScanCameraControl : CLVisionCameraControlBase
    {
        List<ICLVisionCamera> HIKAreaScanList = new List<ICLVisionCamera>();
        public override void SearchDevice(out List<ICLVisionCamera> cLVisionCameraList, out int ErrorCode, out string ErrorText)
        {
            ErrorCode = 0;
            ErrorText = string.Empty;
            cLVisionCameraList = null;

            try
            {
                // ch: 初始化 SDK | en: Initialize SDK
                SDKSystem.Initialize();

                DeviceTLayerType enumTLayerType = DeviceTLayerType.MvGigEDevice | DeviceTLayerType.MvUsbDevice
             | DeviceTLayerType.MvGenTLGigEDevice | DeviceTLayerType.MvGenTLCXPDevice | DeviceTLayerType.MvGenTLCameraLinkDevice | DeviceTLayerType.MvGenTLXoFDevice;

                //设备接口类型为MV_GIGE_DEVICE时，枚举所有GigE设备，包含虚拟GigE设备和采集卡上的GigE设备。 设备接口类型为MV_USB_DEVICE时，枚举所有USB设备，包含虚拟USB设备。
                //设备列表的内存是在SDK内部分配的，调用该接口时会进行设备列表内存的释放和申请，避免多线程枚举操作。 枚举到设备后，通过MvCameraControl.DeviceFactory创建设备实例。
                List<IDeviceInfo> _deviceInfoList;
                int nRet = DeviceEnumerator.EnumDevices(enumTLayerType, out _deviceInfoList);
                if (nRet != MvError.MV_OK)
                {
                    ErrorCode = -1;
                    ErrorText = "Enumerate devices fail!";
                }

                //将相机对象存储到接口中
                foreach (var devInfo in _deviceInfoList)
                {
                    HIKAreaScanCamera hIKAreaScanCamera = new HIKAreaScanCamera(devInfo);
                    hIKAreaScanCamera.CreatDevice(out _, out _);
                    HIKAreaScanList.Add(hIKAreaScanCamera);
                }
                cLVisionCameraList = HIKAreaScanList;
            }
            catch (Exception ex)
            {
                HIKAreaScanList = null;
                ErrorCode = -1;
                ErrorText = "SearchDevice fail!" + ex.Message;
            }
        }


        public override ICLVisionCamera GetHIKAreaScanCamera(int index)
        {
            return HIKAreaScanList[index];
        }

        public override int GetHIKAreaScanCameraCount()
        {
            return HIKAreaScanList.Count;
        }

        public override ICLVisionCameraInfo GetHIKAreaScanCameraInfo(int index)
        {
            return HIKAreaScanList[index].CLVisionCameraInfo;
        }

       
    }
}
